// Loader Functions.
// =================

HashMap<String, pp_sprite> spr = new HashMap<String, pp_sprite>();
HashMap<String, PImage[]> uiSpr = new HashMap<String, PImage[]>();
HashMap<String, pp_font> fnt = new HashMap<String, pp_font>();
HashMap<String, pp_level> lvl = new HashMap<String, pp_level>();

void pp_loader() {
  String path;
  
  // Load Sprites.json
  path = "Sprites/Sprites.json";
  if (pp_fileExists(dataPath(path))) {
    JSONArray data = loadJSONArray(path);
    for (int i = 0; i < data.size(); i++) {    
      JSONObject sprite = data.getJSONObject(i); 
      spr.put(
        sprite.getString("Name"),
        new pp_sprite(
          sprite.getInt("Frames"),
          sprite.getInt("FPS"),
          pp_toBoolean(sprite.getInt("FlipX")),
          pp_toBoolean(sprite.getInt("FlipY")),
          sprite.getString("Sprite"),
          sprite.getString("Bumpmap")
        )
      );
    }
  } else {
    println("JSON file " + dataPath(path) + " not found.");
  }
  
  // Load Effects.json
  path = "Effects/Effects.json";
  if (pp_fileExists(dataPath(path))) {
    JSONArray data = loadJSONArray(path);
    for (int i = 0; i < data.size(); i++) {    
      JSONObject sprite = data.getJSONObject(i); 
      spr.put(
        sprite.getString("Name"),
        new pp_sprite(
          sprite.getInt("Frames"),
          sprite.getInt("FPS"),
          pp_toBoolean(sprite.getInt("FlipX")),
          pp_toBoolean(sprite.getInt("FlipY")),
          sprite.getString("Sprite"),
          sprite.getString("Bumpmap")
        )
      );
    }
  } else {
    println("JSON file " + dataPath(path) + " not found.");
  }
  
  // Load UISprites.json
  path = "UISprites/UISprites.json";
  if (pp_fileExists(dataPath(path))) {
    JSONArray data = loadJSONArray(path);
    for (int i = 0; i < data.size(); i++) {    
      JSONObject sprite = data.getJSONObject(i); 
      PImage[] img = { pp_UISpriteLoad(sprite.getString("Default")), pp_UISpriteLoad(sprite.getString("MouseOver")), pp_UISpriteLoad(sprite.getString("MouseClick")) };
      uiSpr.put(sprite.getString("Name"), img);
    }
  } else {
    println("JSON file " + dataPath(path) + " not found.");
  }
  
  // Load Fonts.json
  path = "Fonts/Fonts.json";
  if (pp_fileExists(dataPath(path))) {
    JSONArray data = loadJSONArray(path);
    for (int i = 0; i < data.size(); i++) {    
      JSONObject font = data.getJSONObject(i); 
      int fontSize = int(font.getInt("FontSize") * (float(height)/900));
      fnt.put(
        font.getString("Name"), 
        new pp_font(
          createFont(font.getString("Filename"), fontSize, false),
          color(font.getInt("FontRed"), font.getInt("FontGreen"), font.getInt("FontBlue")),
          font.getInt("DropShadow")
        )
      );
    }
  } else {
    println("JSON file " + dataPath(path) + " not found.");
  }
  
  // Load Levels.json
  path = "Levels/Levels.json";
  if (pp_fileExists(dataPath(path))) {
    JSONArray data = loadJSONArray(path);
    for (int i = 0; i < data.size(); i++) {
      JSONObject level = data.getJSONObject(i);
      lvl.put(level.getString("Name"), new pp_level(level.getString("Location")));
    }
  } else {
    println("JSON file " + dataPath(path) + " not found.");
  }
  
  // Run construct.
  pp_construct();
  
  // Start game.
  pp_loaded = true;
}

// File Functions.
// ===============

// Check if file exists.
boolean pp_fileExists(String filename) {
  File f = new File(dataPath(filename));
  if (f.exists()) {
    return true;
  } else {
    println("File " + (dataPath(filename)) + " not found");
    return false;
  }
}
